package in.blogspot.khurram2java;
 
import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
 
public class DatabaseHelper extends SQLiteOpenHelper{
 
    private static final String DB_NAME = "bookDB";
    private static final int DB_VERSION = 1;
 
    //คำสั่งในการสร้าง ตารางสำหรับเก็บข้อมูล
    private static final String DB_CREATE = "" +
            "CREATE TABLE book (" +
            "id INTEGER PRIMARY KEY, " +
            "isbn TEXT NOT NULL, " +
            "title TEXT NOT NULL, " +
            "author TEXT NOT NULL, " +
            "publishing TEXT NOT NULL);";
 
    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }
 
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DB_CREATE);
    }
 
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //พิมพ์ Log เพื่อให้เห็นว่ามีการ Upgrade Database
        Log.w(DatabaseHelper.class.getName(),
                "Upgread database version from version" + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
 
        //ลบตาราง book ของเก่าทิ้ง
        db.execSQL("DROP TABLE IF EXISTS book");
        onCreate(db);
    }
    
    public ArrayList<BookData> SelectAllData() {
    	// TODO Auto-generated method stub
    	
    	 try {
    		 ArrayList<BookData> MemberList = new ArrayList<BookData>();
    		 
    		 SQLiteDatabase db;
    		 db = this.getReadableDatabase(); // Read Data
    			
    		 String strSQL = "SELECT  * FROM book" ;
    		 Cursor cursor = db.rawQuery(strSQL, null);
    		 
    		 	if(cursor != null)
    		 	{
    		 	    if (cursor.moveToFirst()) {
    		 	        do {
    		 	        	BookData cMember = new BookData();
    		 	        	cMember.setTitle(cursor.getString(2));
    		 	        	
    		 	        	MemberList.add(cMember);
    		 	        } while (cursor.moveToNext());
    		 	    }
    		 	}
    		 	cursor.close();
    			db.close();
    			return MemberList;
    			
    	 } catch (Exception e) {
    	    return null;
    	 }

    }
    
}